package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.itheima.dao.OrderSettingDao;
import com.itheima.pojo.OrderSetting;
import com.itheima.service.OrderSettingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service(interfaceClass = OrderSettingService.class)
@Transactional
public class OrderSettingServiceImpl implements OrderSettingService {

    @Autowired
    private OrderSettingDao orderSettingDao;

    @Override
    public void add(List<OrderSetting> orderSettingList) {

        for (OrderSetting orderSetting : orderSettingList) {
            //1,先判断是否有该日期
            Long count = orderSettingDao.getCountByOrderDate(orderSetting.getOrderDate());
            if (count>0){
                //2,如果有则修改
                orderSettingDao.updateByOrderDate(orderSetting);
            }else {
                //3,如果没有则添加
                orderSettingDao.add(orderSetting);
            }
        }
    }

    @Override
    public List<Map<String, Integer>> getOrderSettingByMonth(String date) {

        //每个月的起始日

        String datebegin = date + "-01";

        String dateend = date + "-31";

        Map map = new HashMap();
        map.put("datebegin",datebegin);
        map.put("dateend",dateend);



        List<OrderSetting> orderSettingList = orderSettingDao.getOrderSettingByMonth(map);


        List<Map<String,Integer>> list = new ArrayList<>();


        Map<String,Integer> map1 = null;

            for (OrderSetting orderSetting : orderSettingList) {
                map1 = new HashMap<>();
                map1.put("date",orderSetting.getOrderDate().getDate());
                map1.put("number",orderSetting.getNumber());
                map1.put("reservations",orderSetting.getReservations());
                list.add(map1);
            }
        return list;
    }

    @Override
    public void setOrderSettingNumber(OrderSetting orderSetting) {
        if (orderSetting!=null){
            Long count = orderSettingDao.getCountByOrderDate(orderSetting.getOrderDate());

            if (count>0){
                orderSettingDao.updateByOrderDate(orderSetting);
            }else {
                orderSettingDao.add(orderSetting);
            }
        }
    }



    /*
     * 定时清理本月以前预约设置历史数据
     *
     * */
    @Override
    public void clearOldOrderSetting(String firstDayOfThisMonth) {
        orderSettingDao.clearOldOrderSetting(firstDayOfThisMonth);
    }


}
